import 'package:shared_preferences/shared_preferences.dart';
import 'package:synchronized/synchronized.dart';

class CacheUtil {
  static CacheUtil? _singleton;
  static SharedPreferences? preferencesInstance;
  static final Lock _lock = Lock();
  static Future<CacheUtil> getInstance(Function callBack) async {
    if (_singleton == null) {
      await _lock.synchronized(() async {
        if (_singleton == null) {
          // 保持本地实例直到完全初始化。
          var singleton = CacheUtil._();
          await singleton._init();
          _singleton = singleton;
          callBack();
        }
      });
    }
    return _singleton!;
  }

  CacheUtil._();

  Future _init() async {
    preferencesInstance = await SharedPreferences.getInstance();
  }
}
